package com.authine.cloudpivot.engine.service.bizrule.handler;


import java.util.List;
import java.util.Map;

import com.authine.cloudpivot.engine.domain.runtime.BizObject;
import com.authine.cloudpivot.engine.enums.status.DataRowStatus;
import com.authine.cloudpivot.engine.open.domain.bizmodel.BizObjectOptions;
import com.authine.cloudpivot.engine.open.service.OpenEngine;
import com.authine.cloudpivot.engine.service.bizrule.handler.context.Request;

public class 获取子表 extends BaseHandler {

    @Override
    public Object handle(Request request) {
        /* 获取系统开放引擎 */
        OpenEngine engine = this.getOpenEngine(request);
        BizObject bizObject = request.getData().getBizObject();
        /* 获取主表数据 */
        Map<String, Object> datas = bizObject.getData();
        datas.put("class_sort", (Math.random() * 10 + 1));
        /* 获取子表数据 */
        @SuppressWarnings("unchecked")
        List<Map<String, Object>> scores = (List<Map<String, Object>>) datas.get("student_score");
        scores.forEach((m) -> {
            m.put("stu_score", (Math.random() * 100 + 1));
            m.put(BizObject.DATAROWSTATUS, DataRowStatus.Modified);
        });
        engine.updateBizObject(bizObject, new BizObjectOptions(false, false));
        return bizObject;
    }
}
